package com.newproject.resmi;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BuyInstrument extends Activity {
	
	private TextView ticker;
	private TextView name;
	private TextView nin;
	private TextView validUntil;
	private EditText price;
	private EditText amount;
	private EditText total;

	private static final int DATE_DIALOG_ID = 0;
	private int mYear;
    private int mMonth;
    private int mDay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buy_instrument);		
		setTitle(getString(R.string.buying));
		
		Bundle bundle = this.getIntent().getExtras();
		
		ticker = (TextView) findViewById(R.id.instrument_ticker);
		ticker.setText(bundle.getString("ticker"));
		
		name = (TextView) findViewById(R.id.instrument_name);
		name.setText(bundle.getString("name"));
		
		nin = (TextView) findViewById(R.id.instrument_nin);
		nin.setText(bundle.getString("nin"));
		
		price = (EditText) findViewById(R.id.instrument_price);
		amount = (EditText) findViewById(R.id.instrument_amount);
		total = (EditText) findViewById(R.id.instrument_total);
		
		price.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				calculateTotal();
			}
			public void afterTextChanged(Editable arg0) {
				calculateTotal();
				
			}
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				calculateTotal();				
			}			
		});
		
		amount.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				calculateTotal();
			}
			public void afterTextChanged(Editable arg0) {
				calculateTotal();
				
			}
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				calculateTotal();				
			}			
		});
		
		// date picker
        validUntil = (TextView) findViewById(R.id.valid_until);
        Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH) + 1;
        mDay = c.get(Calendar.DAY_OF_MONTH);
        validUntil.setText(" " + mDay + "/" + mMonth + "/" + mYear);
        
		
		Button ok = (Button) findViewById(R.id.button_ok);
        ok.setOnClickListener( new OnClickListener() {
            public void onClick(View v) {
                // magic goes here
          	  Toast toast = Toast.makeText(getApplicationContext(), "You bought this share!",
							Toast.LENGTH_LONG);
          	  toast.show();
          	BuyInstrument.this.finish();
            }
        });
        
        // Cancel button click
        Button cancel = (Button) findViewById(R.id.button_cancel);
        cancel.setOnClickListener( new OnClickListener() {
			public void onClick(View arg0) {
				BuyInstrument.this.finish();
			}        	
        });
	}
	
	public void calculateTotal() {		
		String price_string = price.getText().toString();
		String amount_string = amount.getText().toString();
		
		if (price_string.equals("") && amount_string.equals("")){
			total.setText("");
		}
		
		if (!price_string.equals("") ^ !amount_string.equals("")){
			if(!price_string.equals("")){ 
				total.setText(price_string);
			} else {
				total.setText(amount_string);
			}
		}
		
		if (!price_string.equals("") && !amount_string.equals("")) {
			double p;
			if (price_string.contains(".")) {
				p = Double.valueOf(price_string);
			} else {
				p = Integer.parseInt(price_string);
			}
			
			int a = Integer.parseInt(amount_string);
			
			double t = p*a;
			
			total.setText(t + "");
		}
//		double p = Double.valueOf(price_string);
//		int a = Integer.parseInt(amount_string);
		
//		Log.d("DEBUG", "p = " + p + ", a = " + a);
	}
	
	public void pickDate(View v) {
		showDialog(DATE_DIALOG_ID);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    case DATE_DIALOG_ID:
	        return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
	    }
	    return null;
	}
	
	private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
            };
            
    private void updateDisplay() {
        validUntil.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
            		.append(mDay).append("/")
                    .append(mMonth + 1).append("/")                    
                    .append(mYear).append(" "));
    } 
	
}
